import * as mobx from 'mobx';
import { ListStore } from '../listStore';
import { message } from 'antd';
import * as WORKFLOW_CUSTOMFORM from '../../apis/customReportForm';
import { WeaLocaleProvider } from 'ecCom';

const { observable, action } = mobx;
const getLabel = WeaLocaleProvider.getLabel;

export class WFReportShareStore extends ListStore {

    @observable shareModalVisible = false;    //共享列表
    @observable addShareVisible = false;     //添加共享
    @observable conditions = [];     //添加共享的组件conditions
    reportid = '';     //记录打开的id
    reportname = '';   //记录报表名称
    constructor () {
        super();
        this.toogleAddShare = this.toogleAddShare.bind(this);
        this.toogleShareModal = this.toogleShareModal.bind(this);
        this.addShare = this.addShare.bind(this);
        this.doSearch = this.doSearch.bind(this);
        this.reload = this.reload.bind(this);
    }

    @action

    toogleAddShare (visible = false) {
        if(!visible){
            this.addShareVisible = false;
        }else {
            if(this.conditions.length > 0){
                this.addShareVisible = true;
            }else{
                WORKFLOW_CUSTOMFORM.getCRShareCondition().then((datas)=>{
                    this.conditions = [...datas.conditions];
                    this.addShareVisible = true;
                });
            }
        }
    }

    doSearch (params = {}) {
        let reportid = this.reportid;
        let newParams = {...params,reportid};
        WORKFLOW_CUSTOMFORM.getCRShare(newParams).then((datas)=>{
            const { sessionkey } = datas;
            this.tableStore.getDatas(sessionkey, params.current || 1);
        });
    }

    toogleShareModal (visible = false,params = {}) {
        if(!visible){
            this.shareModalVisible = false;
        }else {
            WORKFLOW_CUSTOMFORM.getCRShare(params).then((datas)=>{
                const { sessionkey } = datas;
                this.tableStore.getDatas(sessionkey, params.current || 1);
                this.reportid = params.reportid;
                this.reportname = params.reportname;
                this.shareModalVisible = true;
            });
        }
    }

    addShare (params) {
        message.loading(getLabel(383498,'正在添加，请稍候...'),0);
        let reportid = this.reportid;
        let newParams = {...params,reportid};
        WORKFLOW_CUSTOMFORM.getCRSaveShare(newParams).then((datas)=>{
            if(datas.success){
                message.destroy();
                message.success(getLabel(83880,'添加成功'));
                this.reload();
            }else {
                message.destroy();
                message.error(getLabel(26772,'添加失败'));
            }
        });
    }

    deleteShare (params) {
        message.loading(getLabel(84024,'正在删除数据，请稍候...'),0);
        WORKFLOW_CUSTOMFORM.getCRDeleteShare(params).then((datas)=>{
            if(datas.success){
                message.destroy();
                message.success(getLabel(20461,'删除成功'));
                this.reload();
            }else {
                message.destroy();
                message.error(getLabel(83473,'删除失败'));
            }
        });
    }

    reload () {
        const current = this.tableStore.current;
        this.doSearch({current});
    }

}